#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>        /*包含头文件。*/
#include <string.h>
#include <iostream>
#include <vector>
#include <map>

#include "common.h"

#define PORT 9999
#define IP "127.0.0.1"

using namespace std;

int main()
{
   struct sockaddr_in addr;
   char mybuff[1024];
   int sock;
   
    
   if((sock = socket(AF_INET, SOCK_STREAM,0))<0)
   {     
        printf("新建Sock 失败");
   }
   
   addr.sin_family = AF_INET;
   addr.sin_addr.s_addr = inet_addr(IP);
   addr.sin_port = htons(9999);
   int errno = 0;
   if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) 
   {
       	fprintf(stderr, "timeout\n");
	return -1;
   }
  printf("connected\n");
 
//  sleep(5);
  LOGIN_MSG loginMsg;

  loginMsg.m_age = 24;
  loginMsg.m_id = 198;
  strcpy( loginMsg.m_msgHead.name, "guanlei");
  loginMsg.m_msgHead.name[sizeof("guanlei")] = '\0';
  memcpy(mybuff,(void*)&loginMsg,sizeof(LOGIN_MSG));
  //int size =   send(sock,(void*)mybuff,sizeof(mybuff),MSG_DONTWAIT);
	int size  = 0;
  if(size>0)
	  fprintf(stderr,"send success\n");

  sleep(2);

//  send(sock,(void*)mybuff,sizeof(mybuff),MSG_DONTWAIT);
  
  //sleep(3);

  USER_LOGIN userLogin;
  strcpy(userLogin.m_sUserName, "guanlei");
  

  strcpy(userLogin.m_sPassWord, "firefly");

  memset(mybuff,0,sizeof(mybuff));

  memcpy(mybuff,(void*)&userLogin,sizeof(USER_LOGIN));

  send(sock, (void*)mybuff, sizeof(mybuff), MSG_DONTWAIT);

  fprintf(stderr,"send somethind");
	
  char revBuff[1024];
 int msgSize =  recv(sock,revBuff,1024,0);
 printf("%d",msgSize);




  close(sock);
  return 0;

}
